import api.base.LoginApi;
import api.base.MessageApi;
import com.alibaba.fastjson2.JSONObject;
import java.util.Base64;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;

public class TestMain {
    public static void main(String[] args) {
        try {
            // 设置控制台编码为UTF-8
            System.setProperty("file.encoding", "UTF-8");
            try {
                java.lang.reflect.Field charset = java.nio.charset.Charset.class.getDeclaredField("defaultCharset");
                charset.setAccessible(true);
                charset.set(null, null);
            } catch (Exception e) {
                // ignore
            }

            System.out.println("开始获取token...");
            
            // 1. 获取token
            JSONObject tokenRes = LoginApi.getToken();
            System.out.println("Token响应: " + tokenRes);
            
            if(tokenRes.getInteger("ret") == 200) {
                String token = tokenRes.getString("data");
                util.OkhttpUtil.setToken(token);
                System.out.println("Token获取成功: " + token);
            } else {
                System.out.println("Token获取失败: " + tokenRes.getString("msg"));
                return;
            }

            System.out.println("开始获取登录二维码...");
            
            // 2. 获取登录二维码
            JSONObject qrRes = LoginApi.getQr("");
            System.out.println("\n完整的二维码响应: " + qrRes);
            
            if(qrRes.getInteger("ret") == 200) {
                JSONObject data = qrRes.getJSONObject("data");
                String qrUrl = data.getString("qrData");
                String qrImgBase64 = data.getString("qrImgBase64");
                String uuid = data.getString("uuid");
                String appId = data.getString("appId");
                
                // 保存二维码图片到临时文件
                if(qrImgBase64 != null && qrImgBase64.contains(",")) {
                    String base64Data = qrImgBase64.split(",")[1];
                    byte[] imageBytes = Base64.getDecoder().decode(base64Data);
                    
                    File tempFile = File.createTempFile("weixin_qr_", ".jpg");
                    try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                        fos.write(imageBytes);
                    }
                    
                    // 打开二维码图片
                    System.out.println("\n正在打开二维码图片...");
                    Desktop.getDesktop().open(tempFile);
                    tempFile.deleteOnExit(); // 程序结束时删除临时文件
                }
                
                // 打印二维码信息
                System.out.println("\n============= 扫码信息 =============");
                System.out.println("请使用微信扫描打开的二维码图片");
                System.out.println("UUID: " + uuid);
                System.out.println("AppID: " + appId);
                System.out.println("====================================\n");
                
                // 3. 等待扫码并检查登录状态
                System.out.println("等待扫码中...");
                boolean isScanned = false;
                boolean isLoggedIn = false;
                
                for(int i = 0; i < 60; i++) {
                    Thread.sleep(2000);
                    JSONObject checkRes = LoginApi.checkQr(appId, uuid, "");
                    
                    // 只在状态变化时打印详细信息
                    if(checkRes.getInteger("ret") == 200) {
                        JSONObject checkData = checkRes.getJSONObject("data");
                        int status = checkData.getInteger("status");
                        
                        switch(status) {
                            case 0: // 未扫码
                                if(!isScanned) System.out.print(".");
                                break;
                            case 1: // 已扫码
                                if(!isScanned) {
                                    System.out.println("\n已扫码，等待确认...");
                                    isScanned = true;
                                }
                                System.out.print(".");
                                break;
                            case 2: // 已确认
                                System.out.println("\n登录成功！");
                                System.out.println("登录信息: " + checkRes);
                                return;
                            case 3: // 已取消
                                System.out.println("\n用户取消登录");
                                return;
                            default:
                                System.out.println("\n未知状态: " + status);
                                System.out.println("详细信息: " + checkRes);
                                break;
                        }
                    } else {
                        System.out.println("\n检查状态失败: " + checkRes);
                    }
                    
                    if(i % 10 == 0 && !isScanned) System.out.println();
                }
            } else {
                System.out.println("获取二维码失败: " + qrRes.getString("msg"));
            }
            
        } catch (Exception e) {
            System.out.println("发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
} 